package com.ruoyi.system.service.impl.usable;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.domain.model.VsUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.usable.VsDramasUsableOrder;
import com.ruoyi.system.domain.user.VsDramasUserWalletLog;
import com.ruoyi.system.mapper.usable.VsDramasUsableOrderMapper;
import com.ruoyi.system.mapper.user.VsDramasUserWalletLogMapper;
import com.ruoyi.system.mapper.user.VsUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.usable.VsDramasUsableMapper;
import com.ruoyi.system.domain.usable.VsDramasUsable;
import com.ruoyi.system.service.usable.IVsDramasUsableService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 积分管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-25
 */
@Service
public class VsDramasUsableServiceImpl implements IVsDramasUsableService 
{
    @Autowired
    private VsDramasUsableMapper vsDramasUsableMapper;

    @Autowired
    private VsUserMapper vsUserMapper;

    @Autowired
    private VsDramasUserWalletLogMapper vsDramasUserWalletLogMapper;

    @Autowired
    private VsDramasUsableOrderMapper vsDramasUsableOrderMapper;

    /**
     * 查询积分管理
     * 
     * @param id 积分管理主键
     * @return 积分管理
     */
    @Override
    public VsDramasUsable selectVsDramasUsableById(Integer id)
    {
        return vsDramasUsableMapper.selectVsDramasUsableById(id);
    }

    /**
     * 查询积分管理列表
     * 
     * @param vsDramasUsable 积分管理
     * @return 积分管理
     */
    @Override
    public List<VsDramasUsable> selectVsDramasUsableList(VsDramasUsable vsDramasUsable)
    {
        return vsDramasUsableMapper.selectVsDramasUsableList(vsDramasUsable);
    }

    /**
     * 新增积分管理
     * 
     * @param vsDramasUsable 积分管理
     * @return 结果
     */
    @Override
    public int insertVsDramasUsable(VsDramasUsable vsDramasUsable)
    {
        vsDramasUsable.setCreatetime(System.currentTimeMillis());
        vsDramasUsable.setUpdatetime(System.currentTimeMillis());
        return vsDramasUsableMapper.insertVsDramasUsable(vsDramasUsable);
    }

    /**
     * 修改积分管理
     * 
     * @param vsDramasUsable 积分管理
     * @return 结果
     */
    @Override
    public int updateVsDramasUsable(VsDramasUsable vsDramasUsable)
    {
        vsDramasUsable.setUpdatetime(System.currentTimeMillis());
        return vsDramasUsableMapper.updateVsDramasUsable(vsDramasUsable);
    }

    /**
     * 批量删除积分管理
     * 
     * @param ids 需要删除的积分管理主键
     * @return 结果
     */
    @Override
    public int deleteVsDramasUsableByIds(Integer[] ids)
    {
        return vsDramasUsableMapper.deleteVsDramasUsableByIds(ids);
    }

    /**
     * 删除积分管理信息
     * 
     * @param id 积分管理主键
     * @return 结果
     */
    @Override
    public int deleteVsDramasUsableById(Integer id)
    {
        return vsDramasUsableMapper.deleteVsDramasUsableById(id);
    }

    @Transactional
    @Override
    public int submitUsable(VsDramasUsable vsDramasUsable) {
        long l = System.currentTimeMillis();
        VsDramasUsable dramasUsable = vsDramasUsableMapper.selectVsDramasUsableById(vsDramasUsable.getId());
        VsUser user = vsUserMapper.selectVsUserById(vsDramasUsable.getUserId());
        VsDramasUserWalletLog vsDramasUserWalletLog = new VsDramasUserWalletLog();
        vsDramasUserWalletLog.setUserId(user.getId());
        BigDecimal wallet = new BigDecimal(dramasUsable.getUsable());
        vsDramasUserWalletLog.setWallet(wallet);
        vsDramasUserWalletLog.setWalletType("usable");
        vsDramasUserWalletLog.setType("admin_recharge");
        BigDecimal after = new BigDecimal(user.getUsable());
        vsDramasUserWalletLog.setBefore(after);
        BigDecimal add = after.add(wallet);
        vsDramasUserWalletLog.setAfter(add);
        vsDramasUserWalletLog.setItemId(String.valueOf(dramasUsable.getId()));
        vsDramasUserWalletLog.setMemo("Backend recharge");
        LoginUser loginUser = SecurityUtils.getLoginUser();
        vsDramasUserWalletLog.setOperId(loginUser.getUserId());
        vsDramasUserWalletLog.setOperType("admin");
        vsDramasUserWalletLog.setCreatetime(l);
        vsDramasUserWalletLog.setUpdatetime(l);
        vsDramasUserWalletLogMapper.insertVsDramasUserWalletLog(vsDramasUserWalletLog);
        VsDramasUsableOrder order = new VsDramasUsableOrder();
        order.setUsableId(dramasUsable.getId());
        String s = "U" + l + IdUtils.fastSimpleUUID();
        order.setOrderSn(s);
        order.setLangId(dramasUsable.getLangId());
        order.setUserId(Math.toIntExact(user.getId()));
        order.setUsable(dramasUsable.getUsable());
        order.setStatus(1);
        order.setTotalFee(dramasUsable.getPrice());
        order.setPayFee(BigDecimal.valueOf(0));
        order.setPayType("system");
        order.setPaytime(l);
        order.setCreatetime(l);
        order.setUpdatetime(l);
        vsDramasUsableOrderMapper.insertVsDramasUsableOrder(order);
        VsUser vsUser = new VsUser();
        vsUser.setId(user.getId());
        vsUser.setUsable(wallet.longValue());
        return vsUserMapper.updateVsUserUsable(vsUser);
    }
}
